<?php
namespace app\admin\controller;

use app\common\cache\user\IUserLevelCache;
use app\common\cache\user\IUserCache;
use app\common\controller\AdminBaseController;
use app\common\enum\Audit;
use app\common\enum\CarStatus;
use app\common\enum\CompanyStatus;
use app\common\enum\HouseStatus;
use app\common\enum\IsHistory;
use app\common\enum\InfoType;
use app\common\enum\LoanStatus;
use app\common\enum\PolicyStatus;
use app\common\enum\UserLoanStatus;
use app\common\ext\IDb;
use app\common\ext\IRequest;

class UserEvaluate extends AdminBaseController{

    /**
     * @功能：额度评估列表
     * @开发者：wdd
     * @return string
     */
    public function user_evaluate_list(){
        //接收参数
        $user_evaluate_id = IRequest::get('user_evaluate_id');
        $nick = IRequest::get('nick');
        $phone = IRequest::get('phone');
        $real = IRequest::get('real');
        $audit_status = IRequest::get('audit_status');
        $is_history = IRequest::get('is_history');

        $user_evaluate_where=null;
        if (!empty($user_evaluate_id)) $user_evaluate_where["ev.user_evaluate_id"]=$user_evaluate_id;
        if (!empty($nick)) $user_evaluate_where["us.nick"]=array("like","%{$nick}%");
        if (!empty($phone)) $user_evaluate_where["us.phone"]=array("like","%{$phone}%");
        if (!empty($real)) $user_evaluate_where["us.real"]=array("like","%{$real}%");
        if (!empty($audit_status)) $user_evaluate_where["ev.audit_status"]=$audit_status;
        if (!empty($is_history)) $user_evaluate_where["ev.is_history"]=$is_history;

        //获取评估列表
        $user_evaluate_list = IDb::getInstance('user_evaluate as ev')
            ->setDbJoin("user as us","ev.user_id = us.user_id")
            ->setDbWhere($user_evaluate_where)
            ->setDbOrder("ev.user_evaluate_id desc")
            ->pag();

        $this->assign('nick',$nick);
        $this->assign('phone',$phone);
        $this->assign('real',$real);
        $this->assign('audit_status',$audit_status);
        $this->assign('is_history',$is_history);
        $this->assign('user_evaluate_id',$user_evaluate_id);
        $this->assign('user_evaluate_list',$user_evaluate_list);
        return view();
    }

    /**
     * @功能：额度评估
     * @开发者：wdd
     * @return string
     */
    public function user_evaluate_assess(){
        //接收参数
        $user_evaluate_id = IRequest::get("user_evaluate_id",IRequest::NOT_EMPTY,"评估编号不能为空！");

        $user_evaluate_info = IDb::getInstance('user_evaluate')->setDbWhere(array('user_evaluate_id'=>$user_evaluate_id))->row();
        if (empty($user_evaluate_info)){
            error('获取额度审核信息失败');
        }
        $user_id = $user_evaluate_info['user_id'];

        //设置查询条件
        $user_info = IUserCache::getInfo($user_id);

        //处理逾期信息
        if(empty($user_info['overdue_info'])){
            $user_info['overdue_info'] = [];
        }else{
            $user_info['overdue_info'] = json_decode($user_info['overdue_info'],true);
        }

        //取得审核人数据
        $user_company_where['user_id'] = $user_id;
        $user_company_where['is_history'] = IsHistory::not;
        $user_company_list = IDb::getInstance('asset_company')->setDbWhere($user_company_where)->sel();

        //取得审核人数据
        $user_car_where['user_id'] = $user_id;
        $user_car_where['is_history'] = IsHistory::not;
        $user_car_list = IDb::getInstance('asset_car')->setDbWhere($user_car_where)->setDbOrder("user_car_id")->sel();

        //取得审核人数据
        $user_house_where['user_id'] = $user_id;
        $user_house_where['is_history'] = IsHistory::not;
        $user_house_list = IDb::getInstance('asset_house')->setDbWhere($user_house_where)->setDbOrder("user_house_id")->sel();

        //取得审核人数据
        $user_credit_card_where['user_id'] = $user_id;
        $user_credit_card_where['is_history'] = IsHistory::not;
        $user_credit_card_list = IDb::getInstance('asset_credit_card')->setDbWhere($user_credit_card_where)->setDbOrder("user_credit_card_id")->sel();

        //取得审核人数据
        $user_policy_where['user_id'] = $user_id;
        $user_policy_where['is_history'] = IsHistory::not;
        $user_policy_list = IDb::getInstance('asset_policy')->setDbWhere($user_policy_where)->setDbOrder("user_policy_id")->sel();

        //取得审核人数据
        $user_loan_where['user_id'] = $user_id;
        $user_loan_where['is_history'] = IsHistory::not;
        $user_loan_list = IDb::getInstance('asset_loan')->setDbWhere($user_loan_where)->setDbOrder("user_loan_id")->sel();

        //页面赋值
        $this->assign("user_info",$user_info);
        $this->assign("user_evaluate_id",$user_evaluate_id);
        $this->assign("overdue_info",$user_info['overdue_info']);
        $this->assign("user_company_list",$user_company_list);
        $this->assign("user_car_list",$user_car_list);
        $this->assign("user_credit_card_list",$user_credit_card_list);
        $this->assign("user_policy_list",$user_policy_list);
        $this->assign("user_house_list",$user_house_list);
        $this->assign("user_loan_list",$user_loan_list);
        $this->assign("user_evaluate_info",$user_evaluate_info);

        //渲染模板输出
        return view("user_evaluate_assess");
    }

    /**
     * @功能：额度评估
     * @开发者：wdd
     * @return string
     */
    public function user_evaluate_yes(){
        $user_evaluate_id = IRequest::get("user_evaluate_id",IRequest::NOT_EMPTY,"评估编号不能为空！");
        $score = IRequest::get("score",IRequest::NOT_EMPTY,"信用评分不能为空！");
        $interest_min = IRequest::get("interest_min",IRequest::NOT_EMPTY,"审核利息最小值不能为空！");
        $interest_max = IRequest::get("interest_max",IRequest::NOT_EMPTY,"审核利息最大值不能为空！");

        $user_evaluate_where['user_evaluate_id'] = $user_evaluate_id;
        $user_evaluate_info = IDb::getInstance('user_evaluate')->setDbWhere($user_evaluate_where)->row();
        if ($user_evaluate_info['audit_status'] != Audit::WaitAudit){
            error("已经评估过了！");
        }

        $extra_data['is_social'] = IRequest::get("data_is_social",IRequest::NOT_EMPTY,"是否有社保不能为空！");
        $extra_data['is_accumulation_fund'] = IRequest::get("data_is_accumulation_fund",IRequest::NOT_EMPTY,"是否有公积金不能为空！");
        $extra_data['is_marry'] = IRequest::get("data_is_marry",IRequest::NOT_EMPTY,"婚姻状况不能为空！");
        $extra_data['is_white'] = IRequest::get("data_is_white",IRequest::NOT_EMPTY,"是否白户不能为空！");
        $extra_data['is_loan'] = IRequest::get("data_is_loan",IRequest::NOT_EMPTY,"是否有过贷款不能为空！");
        $extra_data['is_overdue'] = IRequest::get("data_is_overdue",IRequest::NOT_EMPTY,"是否有逾期记录不能为空！");
        $extra_data['is_student'] = IRequest::get("data_is_student",IRequest::NOT_EMPTY,"是否有逾期记录不能为空！");
        $extra_data['is_car'] = IRequest::get("data_is_car",IRequest::NOT_EMPTY,"是否有车不能为空！");
        $extra_data['is_house'] = IRequest::get("data_is_house",IRequest::NOT_EMPTY,"是否有房产不能为空！");
        $extra_data['is_credit_card'] = IRequest::get("data_is_credit_card",IRequest::NOT_EMPTY,"是否有信用卡不能为空！");
        $extra_data['is_policy'] = IRequest::get("data_is_policy",IRequest::NOT_EMPTY,"是否有保单不能为空！");
        $extra_data['is_company'] = IRequest::get("data_is_company",IRequest::NOT_EMPTY,"是否有公司不能为空！");
        $extra_data['is_assure'] = IRequest::get("data_is_assure",IRequest::NOT_EMPTY,"是否有过担保不能为空！");
        $extra_data['is_shop'] = IRequest::get("data_is_shop",IRequest::NOT_EMPTY,"是否有网店不能为空！");
        $extra_data['is_court'] = IRequest::get("data_is_court",IRequest::NOT_EMPTY,"是否有法院执行记录不能为空！");
        $extra_data['agree_protocol'] = IRequest::get("data_agree_protocol",IRequest::NOT_EMPTY,"是否有法院执行记录不能为空！");

        $house_ids = IRequest::get("house");
        $car_ids = IRequest::get("car");
        $policy_ids = IRequest::get("policy");
        $company_ids = IRequest::get("company");
        $loan_ids = IRequest::get("loan");
        if(empty($house_ids)) $house_ids = [];
        if(empty($car_ids)) $car_ids = [];
        if(empty($policy_ids)) $policy_ids = [];
        if(empty($company_ids)) $company_ids = [];
        if(empty($loan_ids)) $loan_ids = [];

        $is_house = IRequest::get("is_house",IRequest::NOT_EMPTY,"是否拥有不能为空！");
        $house_content = IRequest::get("house_content",IRequest::NOT_EMPTY,"说明不能为空！");
        $house_loan_min = IRequest::get("house_loan_min",IRequest::NOT_EMPTY,"额度最小值不能为空！");
        $house_loan_max = IRequest::get("house_loan_max",IRequest::NOT_EMPTY,"额度最大值不能为空！");
        $house_interest_min = IRequest::get("house_interest_min",IRequest::NOT_EMPTY,"利息最小值不能为空！");
        $house_interest_max = IRequest::get("house_interest_max",IRequest::NOT_EMPTY,"利息最大值不能为空！");

        $is_car = IRequest::get("is_car",IRequest::NOT_EMPTY,"是否拥有不能为空！");
        $car_content = IRequest::get("car_content",IRequest::NOT_EMPTY,"说明不能为空！");
        $car_loan_min = IRequest::get("car_loan_min",IRequest::NOT_EMPTY,"额度最小值不能为空！");
        $car_loan_max = IRequest::get("car_loan_max",IRequest::NOT_EMPTY,"额度最大值不能为空！");
        $car_interest_min = IRequest::get("car_interest_min",IRequest::NOT_EMPTY,"利息最小值不能为空！");
        $car_interest_max = IRequest::get("car_interest_max",IRequest::NOT_EMPTY,"利息最大值不能为空！");

        $is_policy = IRequest::get("is_policy",IRequest::NOT_EMPTY,"是否拥有不能为空！");
        $policy_content = IRequest::get("policy_content",IRequest::NOT_EMPTY,"说明不能为空！");
        $policy_loan_min = IRequest::get("policy_loan_min",IRequest::NOT_EMPTY,"额度最小值不能为空！");
        $policy_loan_max = IRequest::get("policy_loan_max",IRequest::NOT_EMPTY,"额度最大值不能为空！");
        $policy_interest_min = IRequest::get("policy_interest_min",IRequest::NOT_EMPTY,"利息最小值不能为空！");
        $policy_interest_max = IRequest::get("policy_interest_max",IRequest::NOT_EMPTY,"利息最大值不能为空！");

        $is_company = IRequest::get("is_company",IRequest::NOT_EMPTY,"是否拥有不能为空！");
        $company_content = IRequest::get("company_content",IRequest::NOT_EMPTY,"说明不能为空！");
        $company_loan_min = IRequest::get("company_loan_min",IRequest::NOT_EMPTY,"额度最小值不能为空！");
        $company_loan_max = IRequest::get("company_loan_max",IRequest::NOT_EMPTY,"额度最大值不能为空！");
        $company_interest_min = IRequest::get("company_interest_min",IRequest::NOT_EMPTY,"利息最小值不能为空！");
        $company_interest_max = IRequest::get("company_interest_max",IRequest::NOT_EMPTY,"利息最大值不能为空！");

        $is_social = IRequest::get("is_social",IRequest::NOT_EMPTY,"是否拥有不能为空！");
        $social_content = IRequest::get("social_content",IRequest::NOT_EMPTY,"说明不能为空！");
        $social_loan_min = IRequest::get("social_loan_min",IRequest::NOT_EMPTY,"额度最小值不能为空！");
        $social_loan_max = IRequest::get("social_loan_max",IRequest::NOT_EMPTY,"额度最大值不能为空！");
        $social_interest_min = IRequest::get("social_interest_min",IRequest::NOT_EMPTY,"利息最小值不能为空！");
        $social_interest_max = IRequest::get("social_interest_max",IRequest::NOT_EMPTY,"利息最大值不能为空！");

        $is_accumulation_fund = IRequest::get("is_accumulation_fund",IRequest::NOT_EMPTY,"是否拥有不能为空！");
        $accumulation_content = IRequest::get("accumulation_content",IRequest::NOT_EMPTY,"说明不能为空！");
        $accumulation_loan_min = IRequest::get("accumulation_loan_min",IRequest::NOT_EMPTY,"额度最小值不能为空！");
        $accumulation_loan_max = IRequest::get("accumulation_loan_max",IRequest::NOT_EMPTY,"额度最大值不能为空！");
        $accumulation_interest_min = IRequest::get("accumulation_interest_min",IRequest::NOT_EMPTY,"利息最小值不能为空！");
        $accumulation_interest_max = IRequest::get("accumulation_interest_max",IRequest::NOT_EMPTY,"利息最大值不能为空！");


        if (intval($house_loan_min) > intval($house_loan_max) || intval($car_loan_min) > intval($car_loan_max) || intval($policy_loan_min) > intval($policy_loan_max) || intval($company_loan_min) > intval($company_loan_max) || intval($social_loan_min) > intval($social_loan_max) || intval($accumulation_loan_min) > intval($accumulation_loan_max)){
            error("额度最小值不能大于额度最大值！");
        }

        if (intval($interest_min)>intval($interest_max)){
            error("利息最小值不能大于利息最大值！");
        }else if (intval($interest_min)<0 || 99<intval($interest_min)){
            error("利息最小值只能在0到99之间！");
        }else if (intval($interest_max)>100){
            error("利息最大值不能大于100！");
        }

        $loan_min = intval($house_loan_min) + intval($car_loan_min) + intval($policy_loan_min) + intval($company_loan_min) + intval($social_loan_min) + intval($accumulation_loan_min);
        $loan_max = intval($house_loan_max) + intval($car_loan_max) + intval($policy_loan_max) + intval($company_loan_max) + intval($social_loan_max) + intval($accumulation_loan_max);

        //初始化值
        $user_evaluate_where['user_evaluate_id'] = $user_evaluate_id;
        $user_evaluate_data['loan_min'] = floatval($loan_min);
        $user_evaluate_data['loan_max'] = floatval($loan_max);
        $user_evaluate_data['interest_min'] = floatval($interest_min);
        $user_evaluate_data['interest_max'] = floatval($interest_max);
        $user_evaluate_data['audit_status'] = Audit::AuditYes;
        $user_evaluate_data['score'] = $score;
        $user_evaluate_data['audit_admin'] = get_login_admin_id();
        $user_evaluate_data['audit_time'] = get_date_time();
        $user_evaluate_data['house_ids'] = implode(',',$house_ids);
        $user_evaluate_data['car_ids'] = implode(',',$car_ids);
        $user_evaluate_data['policy_ids'] = implode(',',$policy_ids);
        $user_evaluate_data['company_ids'] = implode(',',$company_ids);
        $user_evaluate_data['loan_ids'] = implode(',',$loan_ids);
        $user_evaluate_data['extra_data'] = json_encode($extra_data);

        //开启事物
        IDb::dbStartTrans();

        //改变评估状态
        $user_evaluate_upd = IDb::getInstance('user_evaluate')->setDbWhere($user_evaluate_where)->setDbData($user_evaluate_data)->upd();

        if (empty($user_evaluate_upd)){
            //事物回滚
            IDb::dbRollback();

            error("评估失败！");
        }

        $house_evaluate_data=array('user_evaluate_id'=>$user_evaluate_id,'info_type'=>InfoType::House,'is_own'=>$is_house,'content'=>$house_content,'loan_min'=>$house_loan_min,'loan_max'=>$house_loan_max,'interest_min'=>$house_interest_min,'interest_max'=>$house_interest_max);
        $car_evaluate_data=array('user_evaluate_id'=>$user_evaluate_id,'info_type'=>InfoType::Car,'is_own'=>$is_car,'content'=>$car_content,'loan_min'=>$car_loan_min,'loan_max'=>$car_loan_max,'interest_min'=>$car_interest_min,'interest_max'=>$car_interest_max);
        $policy_evaluate_data=array('user_evaluate_id'=>$user_evaluate_id,'info_type'=>InfoType::Policy,'is_own'=>$is_policy,'content'=>$policy_content,'loan_min'=>$policy_loan_min,'loan_max'=>$policy_loan_max,'interest_min'=>$policy_interest_min,'interest_max'=>$policy_interest_max);
        $accumulation_evaluate_data=array('user_evaluate_id'=>$user_evaluate_id,'info_type'=>InfoType::Accumulation,'is_own'=>$is_accumulation_fund,'content'=>$accumulation_content,'loan_min'=>$accumulation_loan_min,'loan_max'=>$accumulation_loan_max,'interest_min'=>$accumulation_interest_min,'interest_max'=>$accumulation_interest_max);
        $company_evaluate_data=array('user_evaluate_id'=>$user_evaluate_id,'info_type'=>InfoType::Company,'is_own'=>$is_company,'content'=>$company_content,'loan_min'=>$company_loan_min,'loan_max'=>$company_loan_max,'interest_min'=>$company_interest_min,'interest_max'=>$company_interest_max);
        $social_evaluate_data=array('user_evaluate_id'=>$user_evaluate_id,'info_type'=>InfoType::Security,'is_own'=>$is_social,'content'=>$social_content,'loan_min'=>$social_loan_min,'loan_max'=>$social_loan_max,'interest_min'=>$social_interest_min,'interest_max'=>$social_interest_max);

        $house_evaluate_info_add=IDb::getInstance('user_evaluate_info')->setDbData($house_evaluate_data)->add();
        $car_evaluate_info_add=IDb::getInstance('user_evaluate_info')->setDbData($car_evaluate_data)->add();
        $policy_evaluate_info_add=IDb::getInstance('user_evaluate_info')->setDbData($policy_evaluate_data)->add();
        $accumulation_evaluate_info_add=IDb::getInstance('user_evaluate_info')->setDbData($accumulation_evaluate_data)->add();
        $company_evaluate_info_add=IDb::getInstance('user_evaluate_info')->setDbData($company_evaluate_data)->add();
        $social_evaluate_info_add=IDb::getInstance('user_evaluate_info')->setDbData($social_evaluate_data)->add();

        if (empty($house_evaluate_info_add) || empty($car_evaluate_info_add) || empty($policy_evaluate_info_add) || empty($accumulation_evaluate_info_add) || empty($company_evaluate_info_add) || empty($social_evaluate_info_add)){
            //事物回滚
            IDb::dbRollback();

            error("添加失败！");
        }

        if(!empty($house_ids)){
            $result = IDb::getInstance('asset_house')->setDbData(['house_status'=>HouseStatus::OK])->setDbWhere(['user_house_id'=>['in',$house_ids]])->upd();
            if($result === false){
                IDb::dbRollback();
                error("审核客户房产信息失败！");
            }
        }

        if(!empty($car_ids)){
            $result = IDb::getInstance('asset_car')->setDbData(['car_status'=>CarStatus::OK])->setDbWhere(['user_car_id'=>['in',$car_ids]])->upd();
            if($result === false){
                IDb::dbRollback();
                error("审核客户车辆信息失败！");
            }
        }

        if(!empty($policy_ids)){
            $result = IDb::getInstance('asset_policy')->setDbData(['policy_status'=>PolicyStatus::OK])->setDbWhere(['user_policy_id'=>['in',$policy_ids]])->upd();
            if($result === false){
                IDb::dbRollback();
                error("审核客户保单信息失败！");
            }
        }

        if(!empty($company_ids)){
            $result = IDb::getInstance('asset_company')->setDbData(['company_status'=>CompanyStatus::OK])->setDbWhere(['asset_company_id'=>['in',$company_ids]])->upd();
            if($result === false){
                IDb::dbRollback();
                error("审核客户公司信息失败！");
            }
        }

        if(!empty($loan_ids)){
            $result = IDb::getInstance('asset_loan')->setDbData(['loan_status'=>UserLoanStatus::OK])->setDbWhere(['user_loan_id'=>['in',$loan_ids]])->upd();
            if($result === false){
                IDb::dbRollback();
                error("审核客户贷款信息失败！");
            }
        }

        //事物提交
        IDb::dbCommit();
        //画面跳转
        $this->success("操作成功！", Url("user_evaluate_list"));
    }

    /**
     * @功能：额度评估详情
     * @开发者：wdd
     * @return string
     */
    public function user_evaluate_info(){
        //接收参数
        $user_evaluate_id = IRequest::get("user_evaluate_id",IRequest::NOT_EMPTY,"评估编号不能为空！");

        $user_evaluate_info = IDb::getInstance('user_evaluate')->setDbWhere(array('user_evaluate_id'=>$user_evaluate_id))->row();
        if (empty($user_evaluate_info)){
            error('获取额度审核信息失败');
        }

        //获取评估明细
        $user_evaluate_info_list = IDb::getInstance('user_evaluate_info')->setDbWhere(array('user_evaluate_id'=>$user_evaluate_id))->sel();
        if ($user_evaluate_info_list === false){
            error("获取评估明细失败");
        }

        $user_id = $user_evaluate_info['user_id'];

        //设置查询条件
        $user_info = IUserCache::getInfo($user_id);

        //设置layout
        $this->layout("layout_empty");

        $house_ids = explode(",", $user_evaluate_info['house_ids']);
        $car_ids = explode(",", $user_evaluate_info['car_ids']);
        $policy_ids = explode(",", $user_evaluate_info['policy_ids']);
        $company_ids = explode(",", $user_evaluate_info['company_ids']);
        $loan_ids = explode(",", $user_evaluate_info['loan_ids']);
        $extra_data = json_decode($user_evaluate_info['extra_data'],true);

        if(!empty($house_ids)){
            $house = IDb::getInstance('asset_house')->setDbWhere(['user_house_id'=>['in',$house_ids]])->sel();
            if($house === false){
                error("获取客户房产信息失败！");
            }
        }

        if(!empty($car_ids)){
            $car = IDb::getInstance('asset_car')->setDbWhere(['user_car_id'=>['in',$car_ids]])->sel();
            if($car === false){
                error("获取客户车辆信息失败！");
            }
        }

        if(!empty($policy_ids)){
            $policy = IDb::getInstance('asset_policy')->setDbWhere(['user_policy_id'=>['in',$policy_ids]])->sel();
            if($policy === false){
                error("获取客户保单信息失败！");
            }
        }

        if(!empty($company_ids)){
            $company = IDb::getInstance('asset_company')->setDbWhere(['asset_company_id'=>['in',$company_ids]])->sel();
            if($company === false){
                error("获取客户公司信息失败！");
            }
        }

        if(!empty($loan_ids)){
            $loan = IDb::getInstance('asset_loan')->setDbWhere(['user_loan_id'=>['in',$loan_ids]])->sel();
            if($loan === false){
                error("获取客户贷款信息失败！");
            }
        }

        //页面赋值
        $this->assign("user_info",$user_info);
        $this->assign("user_evaluate_info",$user_evaluate_info);
        $this->assign("user_evaluate_info_list",$user_evaluate_info_list);
        $this->assign("house",$house);
        $this->assign("car",$car);
        $this->assign("policy",$policy);
        $this->assign("company",$company);
        $this->assign("loan",$loan);
        $this->assign("extra_data",$extra_data);

        //渲染模板输出
        return view('user_evaluate_info');
    }

}